(function($){
	$.pagination = function(theme, label, optRows){
		var s     = $.pagination.settings;		
		s.label   = label?label:s.label;
		s.theme   = theme;
		s.optRows = optRows && optRows.length>0?optRows:s.optRows;
	}
	
	$.pagination.settings = {
		optRows	: [5,10,25,50,100],
		theme	: '',
		label	: ['First', 'Previous', 'Next', 'Last', 'Per Page', 'Of']
	}
	
	$.pagination.create = function(id, div, frm, content){
		var s 	= $.pagination.settings;
		var l 	= s.label;
		var t 	= s.theme;
		var fL	= l.length>0?l[0]:'First';  
		var pL 	= l.length>1?l[1]:'Previous';
		var nL	= l.length>2?l[2]:'Next';
		var lL	= l.length>3?l[3]:'Last';		
		var pgL	= l.length>4?l[4]:'Per Page';
		var ofL	= l.length>5?l[5]:'Of';
		
		var idx  = $(frm+' #pgIndex').val();
		var rows = $(frm+' #pgRows').val();
		var tots = $(frm+' #pgTotal').val();
			
		var html = '<div id="'+id.substring(1, id.length)+'"><span class="perpage">'+
				   '<select class="rowsperpage"><option></option>'+
				   '</select>&nbsp;'+pgL+'</span>&nbsp;&nbsp;&nbsp;'+
				   '<span class="paginater">&nbsp;'+
				   '<a href="#first" title="'+fL+'" class="firstpage"><img src="'+t+'/first.png"></a>'+
				   '<a href="#prev" title="'+pL+'" class="prevpage"><img src="'+t+'/prev.png"></a>'+
				   '<input class="currpage" type="input" value="'+idx+'" size="1">&nbsp;'+ofL+'&nbsp;'+
				   '<span class="totalpages">'+tots+'</span>&nbsp;'+
				   '<a href="#next" title="'+nL+'" class="nextpage"><img src="'+t+'/next.png"></a>'+
				   '<a href="#last" title="'+lL+'" class="lastpage"><img src="'+t+'/last.png"></a>'+
				   '</span></div>';
		$(div).html(html);
		var sel = $(id+' .rowsperpage')[0];
		var j = 0;
		for (var i=0;i<s.optRows.length;i++){
			sel.options[i] = new Option(s.optRows[i], s.optRows[i], true, true);
			if(rows == s.optRows[i]) j = i;
		}
		sel.options[j].selected = true;
		
		$(id+' .firstpage').click(function() {
			var c = eval($(id+' .currpage').val());
			if(c>1){
				$(frm+' #pgIndex').val(1);
				return $.pagination.execute(id, div, frm, content);
			}
			return false;
	    });		
		
		$(id+' .prevpage').click(function() {
			var c = eval($(id+' .currpage').val());
	  		if(c>1){
	    		$(frm+' #pgIndex').val(c-1);
	    		return $.pagination.execute(id, div, frm, content);
	    	}
	  		return false;
	    });
		
		$(id+' .nextpage').click(function() {
			var c = eval($(id+' .currpage').val());
	  		var m = eval($(id+' .totalpages').text());
	  		if(c<m){
	    		$(frm+' #pgIndex').val(c+1);
	    		return $.pagination.execute(id, div, frm, content);
	    	}
	    });
		
		$(id+' .lastpage').click(function() {
			var c = eval($(id+' .currpage').val());
	  		var m = eval($(id+' .totalpages').text());
	  		if(c<m){
	    		$(frm+' #pgIndex').val(m);
	    		return $.pagination.execute(id, div, frm, content);
	    	}
	  		return false;
	    });
		
		$(id+' .currpage').change(function() {
			var c = eval($(id+' .currpage').val());
	  		var m = eval($(id+' .totalpages').text());
	  		if(c>=1 && c<=m){
	    		$(frm+' #pgIndex').val(m);
	    		return $.pagination.execute(id, div, frm, content);
	    	}
	  		return false;
	    });
		
		$(id+' .rowsperpage').change(function() {
			$(frm+' #pgRows').val($(id+' .rowsperpage').val());
			return $.pagination.execute(id, div, frm, content);
	    });
	}
	
	$.pagination.execute = function(id, div, frm, content){
		$.ajax({
			type	: $(frm).attr('method').toUpperCase(),
			url		: $(frm).attr('action')+'.view',
			data	: $(frm).serialize(),
			success	: function(data) {
				//$(content).html(data);
				//$.pagination.create(id, div, frm, content);
				$(content).flip({ 
					direction: getFlipDirection(),
					color: '#EEEEEE',
					speed:500, 
					content: data,
					onEnd: function(){$.pagination.create(id, div, frm, content)}
				});
			},
			error	: function(data) {
				$.modalbox.showError('Code: '+data.status);
			}
		})
		return false;
	}
})(jQuery);